import 'package:flutter/material.dart';
import 'package:flutter_shop/eventbus/event_bus.dart';
import 'package:flutter_shop/pages/base/base_presenter.dart';
import 'package:flutter_shop/pages/base/base_state.dart';
import 'package:flutter_shop/pages/cart/cart_tab_page/entity/cart_goods_data.dart';
import 'package:flutter_shop/pages/cart/cart_tab_page/provider/cart_provider.dart';
import 'package:flutter_shop/pages/cart/cart_tab_page/widget/cart_bottom_bar.dart';
import 'package:flutter_shop/pages/cart/cart_tab_page/widget/item_cart_goods.dart';
import 'package:flutter_shop/pages/common/index_page/index_page.dart';
import 'package:flutter_shop/r.dart';
import 'package:flutter_shop/res/colors.dart';
import 'package:flutter_shop/widget/common_app_bar.dart';
import 'package:flutter_shop/widget/page_state_widget.dart';
import 'package:provider/provider.dart';

///购物车tab
class CartTabPage extends StatefulWidget {
  static const String SHOW_EMPTY = "CartTabPageShowEmpty";

  @override
  _CartTabPageState createState() => _CartTabPageState();
}

class _CartTabPageState extends BaseState<CartTabPage, BasePresenter> with AutomaticKeepAliveClientMixin {
  PageStateType pageStateType;

  @override
  bool get wantKeepAlive => true;

  @override
  bool get useEventBus => true;

  @override
  void onEventBus(EventMessage event) {
    switch (event.tag) {
      case CartTabPage.SHOW_EMPTY:
        pageStateController.changePageStateType(PageStateType.EMPTY);
        break;
      default:
        break;
    }
  }

  @override
  void initState() {
    super.initState();
    Provider.of<CartProvider>(context, listen: false).getCartGoodsList();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);

    List<CartGoodsData> cartGoodsList = Provider.of<CartProvider>(context).cartGoodsList;

    PageStateType state = cartGoodsList.isEmpty ? PageStateType.EMPTY : PageStateType.SUCCESS;
    pageStateController.changePageStateType(state);

    return Scaffold(
      appBar: CommonAppBar("购物车", showBack: false),
      body: PageStateWidget(
        controller: pageStateController,
        retryWidget: RaisedButton(
          color: CommonColors.primary,
          onPressed: () {
            //随便逛逛
            EventBus().post(EventMessage(IndexPage.CHANGE_INDEX, int1: 0));
          },
          child: Text("随便逛逛", style: TextStyle(color: Colors.white)),
        ),
        state: state,
        image: R.assetsImgCartImgCartEmpty,
        tip: "购物车还空着,快去挑选商品吧",
        child: Container(
          child: Column(
            children: <Widget>[
              Expanded(
                child: ListView.builder(
                  itemCount: cartGoodsList.length,
                  itemBuilder: (context, index) => ItemCartGoods(
                    item: cartGoodsList[index],
                  ),
                ),
              ),
              CartBottomBar(),
            ],
          ),
        ),
      ),
    );
  }
}
